//
//  GMResultVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/11/14.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMResultVC.h"
#import "GMResultCell.h"
#import "GMExamModel.h"
#import "GMAnswerCards.h"
#import "GMMyClassVC.h"
#import "GMHomeVC.h"
#import "GMAnalysisVC.h"
#import "GMExercisesVC.h"
#import "GMPracticeViewController.h"
@interface GMResultVC ()<XMGWaterflowLayoutDelegate>
/**标题*/
@property (weak, nonatomic) IBOutlet UILabel *titleName;
/**正确率*/
@property (weak, nonatomic) IBOutlet UILabel *correctRate;
/**总共用时*/
@property (weak, nonatomic) IBOutlet UILabel *totalTime;
/**平均用时*/
@property (weak, nonatomic) IBOutlet UILabel *averageTime;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) GMExamModel *baseModel;

@end

@implementation GMResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"答题结果";
    //覆盖返回按键
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithImage:[UIImage imageNamed:@"left-white-64"] WithHighlightedImage:[UIImage imageNamed:@"left-gray-64"] Target:self action:@selector(backItemClick) title:@"  "];

    XMGWaterflowLayout *layout = [[XMGWaterflowLayout alloc] init];
    layout.delegate = self;
    self.collectionView.collectionViewLayout =layout;
    [self.collectionView registerNib:[UINib nibWithNibName:@"GMResultCell" bundle:nil] forCellWithReuseIdentifier:@"GMResultCell"];
    [self getGMResultData];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(void)getGMResultData{
    NSDictionary *dict= @{@"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"examId":self.examId,
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetExamResult&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                GMExamModel *model = [GMExamModel mj_objectWithKeyValues:[responseObject objectForKey:RESULT_NAME_Data]];
                self.baseModel = model;
                self.titleName.text = model.Title;
                self.correctRate.text = [NSString stringWithFormat:@"%@",model.CorrectRate];
                self.totalTime.text = [NSString stringWithFormat:@"%ld",model.UseTime];
                self.averageTime.text = model.AverageTime;
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:model.AnswerCards];
                [self.collectionView reloadData];
            }
        }else{
            UIAlertController *alert  = [UIAlertController zj_alertControllerWithTitle:@"提示" message:[responseObject objectForKey:RESULT_NAME_Message] optionStyle:OptionStyleStyleOnlyOK OkTitle:@"好的" cancelTitle:@"" okBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } cancelBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMResultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMResultCell" forIndexPath:indexPath];
    GMAnswerCards *model = self.dataArr[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - <XMGWaterflowLayoutDelegate>
- (CGFloat)waterflowLayout:(XMGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth{
    //计算item的高度
    return itemWidth;
}
/**行间距*/
- (CGFloat)rowMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 10;
}
/**列间距*/
-(CGFloat)columnMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 10;
}
/**返回每行的个数*/
- (CGFloat)columnCountInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return 5;
}
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GMAnswerCards *model = self.dataArr[indexPath.row];
    GMAnalysisVC *analysis = [[GMAnalysisVC alloc] init];
    analysis.baseModel = self.baseModel;
    analysis.index = [model.SortNo integerValue];
    [self.navigationController pushViewController:analysis animated:YES];
    
}
/**返回*/
-(void)backItemClick{
    // 返回到指定
    for (UIViewController *temp in self.navigationController.viewControllers) {
        if ([temp isKindOfClass:[GMMyClassVC class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
        if ([temp isKindOfClass:[GMHomeVC class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
        if ([temp isKindOfClass:[GMPracticeViewController class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }
}
/**查看解析*/
- (IBAction)lookbtn:(id)sender {
    GMAnalysisVC *analysis = [[GMAnalysisVC alloc] init];
    analysis.examId= self.examId;
    analysis.baseModel = self.baseModel;
    [self.navigationController pushViewController:analysis animated:YES];
}
/**继续练习*/
- (IBAction)continuebtn:(id)sender {
    // 返回到指定
    GMExercisesVC *exercise = [[GMExercisesVC alloc] init];
    exercise.resId = self.baseModel.ResId;
    [self.navigationController pushViewController:exercise animated:YES];
    
}

@end
